package service.clazz.strategy;

import entity.table.TableDto;
import entity.table.TableFileDto;
import entity.table.TableInfoDto;
import enums.table.ClazzTypeEnum;
import enums.table.InitImportEnum;
import service.clazz.interfaces.ClazzStrategy;
import utils.io.FileUtil;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 实体类
 */
public class ClazzServiceImplStrategy extends ClazzStrategy {

    @Override
    public TableFileDto enerateStrategy(List<TableDto> list, TableInfoDto table) {
        TableFileDto tableFileDto = new TableFileDto();
        tableFileDto.setFileName(table.getTableNameCamel(ClazzTypeEnum.SERVICEIMPL)+".java");
        tableFileDto.setContext(this.bringClazz(list, table));
        return tableFileDto;
    }

    /**
     * 生成类
     */
    public String bringClazz(List<TableDto> list, TableInfoDto table) {
        StringBuilder str = new StringBuilder();

        // 类路径
        str.append(super.bringPackage(table.getServicePackaging()));
        // 类引用
        str.append(super.bringImport(this.importList(table))).append("\n");
        // 类内容
        // @Service("orderService")
        // public class OrderServiceImpl implements OrderService {
        //	Logger logger = LoggerFactory.getLogger(OrderServiceImpl.class);
        str.append("@Component");
        str.append(super.bringBody(ClazzTypeEnum.SERVICEIMPL, table.getTableNameCamel(), this.body(table)));
        // 输出
        return str.toString();
    }


    private List<String> importList(TableInfoDto table) {
        List<String> list = InitImportEnum.initEntityList();
        list.add(InitImportEnum.Page.getClazzImport());
        list.add(InitImportEnum.Logger.getClazzImport());
        list.add(InitImportEnum.LoggerFactory.getClazzImport());
        list.add(InitImportEnum.Autowired.getClazzImport());
        list.add(InitImportEnum.Component.getClazzImport());
        list.add(InitImportEnum.StrUtil.getClazzImport());
        list.add(table.getEntityPackaging() + ".*");
        list.add(table.getDaoPackaging() + "." + table.getTableName() + "." + table.getTableNameCamel(ClazzTypeEnum.DAO));
        list.add(table.getServicePackaging() + "." + table.getTableName() + "." + table.getTableNameCamel(ClazzTypeEnum.SERVICE));
        return list;
    }

    private String body(TableInfoDto table) {
        return super.codeTemple("ServiceImpl", table);
    }
}
